<template>
    <div class='view-page'>
        <div class="header">
            <div class="content">
                <!-- 博客页面标题 -->
                <div class="blog-header">
                    <h1>Harvest Gain Blog</h1>
                </div>

                <!-- 分类标签区域 -->
                <div class="category-tags">
                    <button class="category-tag" :class="{ 'active': activeCategoryId === null }"
                        @click="handlerChangeCategory(null)">All Blogs</button>
                    <button class="category-tag" v-for="item in categoryList" :key="item.id"
                        @click="handlerChangeCategory(item.id)" :class="{ 'active': activeCategoryId === item.id }">{{
                            item.name }}</button>
                </div>
            </div>
        </div>

        <!-- 博客文章列表 -->
        <div class="blog-articles">
            <!-- 博客文章1 -->
            <div class="blog-article" v-for="item in blogList" :key="item.id" @click="handleClick(item)">
                <div class="article-image">
                    <img :src="item.cover" :alt="item.name" loading="lazy">
                </div>
                <div class="article-category" v-if="filterCategoryName(item.categoryId)">{{
                    filterCategoryName(item.categoryId) }}</div>
                <h2 class="article-title">{{ item.title }}</h2>
                <p class="article-excerpt">{{ item.description }}</p>
            </div>
        </div>
        <div style="width: 100%;display: flex;justify-content: center;margin-top:30px"
            v-if="total > 4 && blogList.size < total">
            <button class="load-more-btn" @click="handleLoadMore">Load More</button>
        </div>
    </div>
</template>

<script>
import { pageSelectBlogList, listSelectBlogCategory } from '@/config/api';

export default {
    computed: {
        filterCategoryName() {
            return (categoryId) => {
                return this.categoryList.find((item) => item.id === categoryId)?.name || '';
            }
        }
    },
    data() {
        return {
            // 可以在这里添加数据，如博客文章列表、选中的分类等
            categoryList: [],
            blogList: [],
            total: 0,
            requestParam: {
                pageNum: 1,
                pageSize: 8,
                categoryId: null,
            },
            activeCategoryId: null,
        };
    },
    created() {
        // 可以在这里添加页面加载后的逻辑
        this.loadBlogCategoryList();
        this.getBlogList();
    },
    methods: {
        // 可以在这里添加方法，如切换分类、处理点击事件等
        loadBlogCategoryList() {
            listSelectBlogCategory().then((res) => {
                if (res.code === 200) {
                    this.categoryList = res.data;
                }
            });
        },
        getBlogList() {
            pageSelectBlogList(this.requestParam).then((res) => {
                if (res.code === 200) {
                    this.blogList = [...this.blogList, ...res.data.records];
                    this.total = res.data.total;
                }
            });
        },
        handleLoadMore() {
            this.requestParam.pageNum++;
            this.getBlogList();
        },
        handleClick(item) {
            this.$router.push({
                path: '/blogDetail',
                query: {
                    id: item.id,
                }
            });
        },
        handlerChangeCategory(id) {
            this.requestParam.categoryId = id;
            this.activeCategoryId = id;
            this.blogList = []
            this.getBlogList();
        }
    },
}
</script>

<style scoped lang="less">
.view-page {
    width: 100%;
    background: #F7F8F9;
}

.header {
    width: 100%;
    background: #f7f4ee;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-bottom: 71px;

    .content {
        padding-top: 40px;
        max-width: 1380px;
        padding-left: 124px;
        padding-right: 124px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
}

/* 博客页面标题样式 */
.blog-header {
    text-align: center;
    margin-bottom: 40px;
}

.blog-header h1 {
    font-size: 32px;
    font-weight: 600;
    color: #333;
    margin: 0;
}

/* 分类标签样式 */
.category-tags {
    display: flex;
    flex-wrap: wrap;
    //justify-content: space-between;
    gap: 12px;
    margin-bottom: 60px;
}

.category-tag {
    border: 1px solid #34773f;
    background-color: #ffffff00;
    color: #34773f;
    cursor: pointer;
    font-weight: bold;
    font-size: 15px;
    transition: all 0.3s ease;
    padding: 13px 50px;
    border-radius: 50px;
    width: 250px;
}

.category-tag:hover {
    background-color: #34773f;
    color: #ffffff;
}

.category-tag.active {
    background-color: #34773f;
    color: #fff;
    border-color: #34773f;
}

/* 博客文章列表样式 */
.blog-articles {
    padding-top: 50px;
    padding-bottom: 60px;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    flex-wrap: wrap;
    justify-content: center;
    gap: 40px;
    max-width: 1380px;
    padding-left: 124px;
    padding-right: 124px;
    margin: 0 auto;
}

.blog-article {
    border: 1px solid #eee;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.blog-article:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.article-image {
    width: 100%;
    height: 300px;
    overflow: hidden;
}

.article-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.blog-article:hover .article-image img {
    transform: scale(1.05);
}

.article-category {
    padding: 15px 20px 0;
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    color: #809e2c;
    letter-spacing: 1px;
}

.article-title {
    padding: 10px 20px;
    font-size: 20px;
    font-weight: 600;
    color: #333;
    margin: 0;
    line-height: 1.4;
}

.article-excerpt {
    padding: 0 20px 20px;
    font-size: 14px;
    color: #666;
    line-height: 1.6;
    margin: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .blog-articles {
        grid-template-columns: 1fr;
    }

    .article-image {
        height: 250px;
    }

    .category-tags {
        gap: 8px;
    }

    .category-tag {
        padding: 6px 12px;
        font-size: 12px;
    }
}


.load-more-btn {
    border: 1px solid #34773f;
    background-color: transparent;
    color: #34773f;
    border-radius: 50px;
    font-weight: 700;
    font-size: 18px;
    padding: 20px 20px;
    border-radius: 34px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    width: 250px;
    margin: 30px auto;
}

.load-more-btn:hover {
    background-color: #34773f;
    color: white;
}
</style>
